package org.strieber.fca;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TabbedFileManipulator {

	private List<ArrayList<String>> fileContents = new ArrayList<ArrayList<String>>();
	
	// create new TabbedFileManipulator from contents of file
	public TabbedFileManipulator(File file) throws FileNotFoundException {
		Scanner scanner = new Scanner(file);
		while (scanner.hasNext()) {
			processLine(scanner.nextLine());
		}
		fileContents.remove(1);  // remove blank line
	}	
	
	// process line from file (used internally)
	protected void processLine(String line) {
		Scanner scanner = new Scanner(line);
		scanner.useDelimiter("\t");
		ArrayList<String> currentLine = new ArrayList<String>();
		while (scanner.hasNext()) {
			currentLine.add(scanner.next());
		}
		fileContents.add(currentLine);
	}
	
	// return fileContents
	public List<ArrayList<String>> getFileContents() {
		return fileContents;
	}
	
	// remove attribute and all associated attribute data
	public void removeAttribute(String attributeName) {
		List<String> headerList = fileContents.get(0);
		int attributeLocation = headerList.indexOf(attributeName);
		if (attributeLocation >= 0) {
			for (int i = 0; i < fileContents.size(); i++) {
				fileContents.get(i).remove(attributeLocation);
			}
		}
	}
	
	// remove object and all associate data
	public void removeObject(int objectNumber) {
		// avoid removing header
		int translatedNumber = objectNumber + 1;
		fileContents.remove(translatedNumber);
	}
	
	// save TabbedFile to file system
	public void exportTabbedFile(File file) throws IOException {
		StringBuilder currentLine = new StringBuilder();
		BufferedWriter out = new BufferedWriter(new FileWriter(file, false)); 
		ArrayList<String> currentList;
		int currentListSize;
		int fileContentsSize = fileContents.size();
		for (int i = 0; i < fileContentsSize; i++) {
			if (i == 1) out.write("\n"); // insert blank line back in
			currentList = fileContents.get(i);
			currentListSize = currentList.size();
			// build currentLine with tabs between elements
			for (int j = 0; j < currentListSize; j++) {
				currentLine.append(currentList.get(j));
				// avoid extra tab at end of each line
				if (j != (currentListSize - 1)) {
					currentLine.append("\t");
				}
			}
			// avoid extra new line at end of each document
			out.write(currentLine.toString());
			// ready the string builder for the next line
			currentLine.delete(0, currentLine.length());
			if (i != (fileContentsSize - 1)) {
				out.write("\n");
			}
		}
		out.close();
	}
	
	// return header containing all attributes as ArrayList<String>
	public ArrayList<String> getHeader() {
		return fileContents.get(0);
	}
	
	// return the data from an object
	public ArrayList<String> getObjectData(int n) {
		return fileContents.get(n);
	}
	
	public static void main(String[] args) throws Exception {
		String defaultFileName = "masterFile.txt";
		String defaultSaveName = "savedFile.txt";
		File file = new File(defaultFileName);
		TabbedFileManipulator tfm = new TabbedFileManipulator(file);
		
		List<ArrayList<String>> testList = tfm.getFileContents();
		tfm.removeAttribute("DIST=0");
		List<String> currentList = new ArrayList<String>();
		System.out.println("Printing contents after attribute removal:");
		for (int i = 0; i < testList.size(); i++) {
			currentList = testList.get(i);
			for (int j = 0; j < currentList.size(); j++) {
				String currentString = currentList.get(j);
				System.out.println("(" + i + ", " + j + "): " + currentString);
			}
		}
		File saveFile = new File(defaultSaveName);
		System.out.println("Saving file: " + defaultSaveName);
		tfm.exportTabbedFile(saveFile);
	}
}